/*
███████╗██╗   ██╗ ██████╗██╗  ██╗
██╔════╝██║   ██║██╔════╝██║ ██╔╝
█████╗  ██║   ██║██║     █████╔╝ 
██╔══╝  ██║   ██║██║     ██╔═██╗ 
██║     ╚██████╔╝╚██████╗██║  ██╗
╚═╝      ╚═════╝  ╚═════╝╚═╝  ╚═╝
*/
package Service;

import dao.PaymentDao;
import dao.EX_UserDao;
import common.JavaBean.EX_User;
import common.utils.PageInfo;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class PaymentService {
    private PaymentDao paymentDao = new PaymentDao();
    private EX_UserDao EX_UserDao = new EX_UserDao();

    // 管理员功能
    public boolean adminRecharge(int adminId, int targetUserId, BigDecimal amount) {
        try {
            return "SUCCESS".equals(paymentDao.adminRecharge(adminId, targetUserId, amount));
        } catch (SQLException e) {
            throw new RuntimeException("充值失败", e);
        }
    }

    public boolean adminManageWallet(int userId, String newPin, Integer status) {
        try {
            return "SUCCESS".equals(paymentDao.adminManageWallet(userId, newPin, status));
        } catch (SQLException e) {
            throw new RuntimeException("管理钱包失败", e);
        }
    }

    public boolean updateUserinfo(Integer userid, String newPassword, String newPhone,
                                  String newAddress, String newEmail, String newDescription) {
        try {
            EX_UserDao.updateUser(userid, newPassword, newPhone, newAddress, newEmail, newDescription);
            return true;
        } catch (SQLException e) {
            throw new RuntimeException("更新用户信息失败", e);
        }
    }

    public List<EX_User> searchUsers(Integer id, String keyword, String phone, String email, Integer permission) {
        try {
            ResultSet rs = EX_UserDao.findUsers(id, keyword, phone, email,permission==null?null:permission);
            List<EX_User> users = new ArrayList<>();
            while (rs.next()) {
                EX_User user = new EX_User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setMoney(rs.getBigDecimal("money"));
                user.setPermission(rs.getInt("permission"));
                user.setPhone(rs.getString("phone"));
                user.setEmail(rs.getString("email"));
                users.add(user);
            }
            return users;
        } catch (SQLException e) {
            throw new RuntimeException("查询用户失败", e);
        }
    }

    public int getBalanceByUserId(Integer userId) {
        try {
            try (ResultSet rs = paymentDao.findWalletByUserId(userId)) {
                if (rs.next()) {
                    return rs.getBigDecimal("balance")
                            .multiply(BigDecimal.valueOf(100))
                            .intValue();
                }
            }
            return -1;
        } catch (SQLException e) {
            throw new RuntimeException("查询钱包余额失败", e);
        }
    }

    // 用户功能
    public boolean setPaymentPin(int userId, String oldPin, String newPin) {
        try {
            return "SUCCESS".equals(paymentDao.setPaymentPin(userId, oldPin, newPin));
        } catch (SQLException e) {
            throw new RuntimeException("设置支付密码失败", e);
        }
    }

    public boolean processPayment(int userId, int orderId, String pin) {
        try {
            return "SUCCESS".equals(paymentDao.processPayment(userId, orderId, pin));
        } catch (SQLException e) {
            throw new RuntimeException("支付失败", e);
        }
    }
}
